<?php
    $url = "localhost:3306";
    $user = "root";
    $pwd = "";
    $database = "php";
    // 1.获取连接
    $conn = mysqli_connect($url, $user, $pwd, $database);

    // 设置 当前连接的编码格式 (字符集), 防止中文乱码
    mysqli_query($conn, "set names utf8");

    $name = "考拉2";
    $price = 29.9;
    $unit = "只";
    $image_url = "/images/Koala.jpg";
    // 2.添加
    $sql = "insert into product ".
    " (p_name, p_price, p_unit, p_image_url) ".
    " values ".
    " ('$name', $price, '$unit', '$image_url') ";
    // 如果是 增、删、改 操作，执行成功后，返回true；否则返回false。
    $result = mysqli_query($conn, $sql);
    if($result) {
        echo "执行成功<br>";
        // 获取添加操作返回的主键
        $key = mysqli_insert_id($conn);
        echo "当前商品的p_id=$key";
    } else {
        echo "执行失败:" . mysqli_error($conn);
    }

    // 3.关闭连接(释放资源)
    mysqli_close($conn);

?>